﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.FeedbackModule.Entity;

namespace Ivy.FeedbackModule.DataAccess
{
    public class FeedbackDao : IDaoInterface<Feedback, FeedbackCriteria>
    {
        #region Variables & Constants
        #endregion Variables & Constants

        #region Constructor & Destructor
        public FeedbackDao() : base() { }
        //public FeedbackDao(Feedback Feedback)
        //{
        //    this.FeedbackKey = Feedback.FeedbackKey;
        //    this.FeedbackUserKey = Feedback.FeedbackUserKey;
        //    this.FeedbackText = Feedback.FeedbackText;
        //    this.FeebackDate = Feedback.FeebackDate;

        //    this.Sys_CreatedByID = Feedback.Sys_CreatedByID;
        //    this.Sys_DateOfCreation = Feedback.Sys_DateOfCreation;
        //    this.Sys_DateOfModification = Feedback.Sys_DateOfModification;
        //    this.Sys_DeleteType = Feedback.Sys_DeleteType;
        //    this.Sys_InitialPrimaryKeys = Feedback.Sys_InitialPrimaryKeys;
        //    this.Sys_IsDeleted = Feedback.Sys_IsDeleted;
        //    this.Sys_ModifiedByID = Feedback.Sys_ModifiedByID;
        //    this.Sys_OrganizationID = Feedback.Sys_OrganizationID;
        //    this.Sys_WorkstationName = Feedback.Sys_WorkstationName;
        //    this.ConcurrencyTimeStamp = Feedback.ConcurrencyTimeStamp;
        //    this.EntityState = Feedback.EntityState;
        //}

        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        #region IDaoInterface<Feedback, FeedbackCriteria> Members
        ///<summary>
        /// Created by Kambui Nurse
        /// </summary>
        /// <returns></returns>
        public bool Insert(Feedback feedbackEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                if (feedbackEntity.EntityState == Common.Framework.Base.EntityState.New)
                {
                    DbConnection dbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand cmdObj = DataAccessProviderUtility.CreateCommand();
                    cmdObj.CommandText = "Ivy_Feedback_Insert";
                    cmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(cmdObj, "@FeedbackUserKey", ParameterDirection.Input, DbType.Int32, feedbackEntity.FeedbackUserKey);
                    DataAccessProviderUtility.AddParameter(cmdObj, "@FeedbackText", ParameterDirection.Input, DbType.String, feedbackEntity.FeedbackText);
                    DataAccessProviderUtility.AddParameter(cmdObj, "@FeedbackDate", ParameterDirection.Input, DbType.DateTime, feedbackEntity.FeebackDate);

                    DatabaseCommon.InsertStoredProcForParameterDatEntityBase(cmdObj, feedbackEntity);

                    DataAccessProviderUtility.AddParameter(cmdObj, "@FeedbackKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                    DataAccessProviderUtility.AddParameter(cmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                    cmdObj.Connection = dbConn;
                    dbConn.Open();
                    cmdObj.Prepare();
                    int _NoOfRecords = cmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        feedbackEntity.FeedbackKey = Convert.ToInt32(cmdObj.Parameters["@FeedbackKey"].Value);
                        feedbackEntity.Sys_DateOfCreation = Convert.ToDateTime(cmdObj.Parameters["@Sys_DateOfCreation"].Value);
                        feedbackEntity.Sys_DateOfModification = feedbackEntity.Sys_DateOfCreation;
                        feedbackEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    cmdObj.Dispose();
                    dbConn.Close();
                }
                else
                {
                    throw new Exception("For inserting new Feedback entity data to database,[EntityState] attribute should be [New]");
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }

            return IsOperationSuccessful;
        }

        public bool Update(Feedback feedbackEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                if (feedbackEntity.EntityState == Common.Framework.Base.EntityState.Changed)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Ivy_Feedback_Update";
                    CmdObj.CommandType = CommandType.StoredProcedure;
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackKey", ParameterDirection.Input, DbType.Int32, feedbackEntity.FeedbackKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserKey", ParameterDirection.Input, DbType.Int32, feedbackEntity.FeedbackUserKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackDate", ParameterDirection.Input, DbType.DateTime, feedbackEntity.FeebackDate);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackText", ParameterDirection.Input, DbType.String, feedbackEntity.FeedbackText);

                    DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, feedbackEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        feedbackEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                        feedbackEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {
                    throw new Exception("For updating existing Feedback entity data to database, [EntityState] attribute should be [Changed]");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool Delete(Feedback feedbackEntity)
        {
            bool IsOperationSuccessfull = false;
            try
            {
                feedbackEntity.EntityState = EntityState.Changed;
                feedbackEntity.Sys_IsDeleted = true;
                if (Update(feedbackEntity))
                {
                    IsOperationSuccessfull = true;
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);

                throw ex;
            }

            return IsOperationSuccessfull;
        }

        public Feedback GetByID(string ID)
        {
            Feedback lSelectedFeedback = null;
            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Ivy_Feedback_GetByID";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ID));
                //DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_IsDeleted", ParameterDirection.Input, DbType.Boolean, this.Sys_IsDeleted);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_OrganizationID", ParameterDirection.Input, DbType.Int32, this.Sys_OrganizationID);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                DbDataReader _DataReader = CmdObj.ExecuteReader();

                if (_DataReader.HasRows)
                {
                    if (_DataReader.Read())
                    {
                        lSelectedFeedback = new Feedback();
                        TranslateFeedbackFieldsFromReaderToEntity(lSelectedFeedback, _DataReader);

                    }
                }
                else
                {
                    throw new Exception("No Feedback Found");
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return lSelectedFeedback;
        }

        public List<Feedback> GetAll()
        {
            var lFeedbackList = new List<Feedback>();
            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Ivy_Feedback_GetAll";
                CmdObj.CommandType = CommandType.StoredProcedure;

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        var lFeedbacktemp = new Feedback();
                        this.TranslateFeedbackFieldsFromReaderToEntity(lFeedbacktemp, _DataReader);
                        lFeedbackList.Add(lFeedbacktemp);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return lFeedbackList;
        }

        public List<Feedback> GetByCriteria(FeedbackCriteria criteriaEntity)
        {
            var lFeedbackList = new List<Feedback>();
            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "sp_Feedback_GetByCriteria";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserKey", ParameterDirection.Input, DbType.Int32, criteriaEntity.FeedbackUserKey);
                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackText", ParameterDirection.Input, DbType.String, criteriaEntity.FeedbackText);
                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackDate", ParameterDirection.Input, DbType.DateTime, criteriaEntity.FeedbackText);

                DatabaseCommon.DefaultStoredProcParameterValuesForGetByCriteria(CmdObj, criteriaEntity);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        Feedback lFeedbackTemp = new Feedback();
                        this.TranslateFeedbackFieldsFromReaderToEntity(lFeedbackTemp, _DataReader);
                        lFeedbackList.Add(lFeedbackTemp);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return lFeedbackList;
        }

        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        /// <summary>
        /// Created by Kambui
        /// </summary>
        /// <param name="pFeedback"></param>
        /// <param name="pDbDataReader"></param>
        private void TranslateFeedbackFieldsFromReaderToEntity(Feedback pFeedback, DbDataReader pDbDataReader)
        {
            try
            {
                pFeedback.FeedbackKey = Convert.ToInt32(pDbDataReader["FeedbackKey"]);
                pFeedback.FeedbackUserKey = Convert.ToInt32(pDbDataReader["FeedbackUserKey"]);
                pFeedback.FeedbackText = Convert.ToString(pDbDataReader["FeedbackText"]);
                pFeedback.FeebackDate = Convert.ToDateTime(pDbDataReader["FeedbackDate"]);

                DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pFeedback, pDbDataReader);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
        }

        //private void CopyValueFromEntityToADO(Feedback pFeedback)
        //{
        //    try
        //    {
        //        this.FeedbackKey = pFeedback.FeedbackKey;
        //        this.FeedbackUserKey = pFeedback.FeedbackUserKey;
        //        this.FeedbackText = pFeedback.FeedbackText;
        //        this.FeebackDate = pFeedback.FeebackDate;

        //        this.Sys_CreatedByID = pFeedback.Sys_CreatedByID;
        //        this.Sys_DateOfCreation = pFeedback.Sys_DateOfCreation;
        //        this.Sys_DateOfModification = pFeedback.Sys_DateOfModification;
        //        this.Sys_DeleteType = pFeedback.Sys_DeleteType;
        //        this.Sys_InitialPrimaryKeys = pFeedback.Sys_InitialPrimaryKeys;
        //        this.Sys_IsDeleted = pFeedback.Sys_IsDeleted;
        //        this.Sys_ModifiedByID = pFeedback.Sys_ModifiedByID;
        //        this.Sys_OrganizationID = pFeedback.Sys_OrganizationID;
        //        this.Sys_WorkstationName = pFeedback.Sys_WorkstationName;
        //        this.ConcurrencyTimeStamp = pFeedback.ConcurrencyTimeStamp;
        //        this.EntityState = pFeedback.EntityState;
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Diagnostics.Debug.Print(ex.Message);
        //        throw;
        //    }  
        //}

        #endregion Private Methods


    }
}
